$().ready(function(){
	
	$baseUrl = $("#baseUrl").text();
	
	$('#keywordsFetch').bind('keypress', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		 if(code == 13) { //Enter keycode
			 $("#searchFetch").click();
		 }
	});

	
	$("#searchFetch,.pagination a").live("click", function(){
		var page = 1;
		var id = $(this).attr("id");
		if( id == "next" || id == "prev")
			page = $(this).attr("class");
		$.ajax({
    		  url: $baseUrl + '/book/fetchajax',
    		  type: 'post',
    		  dataType: 'json',
    		  async: true,
    		  data: ({'keyword' : $("#keywordsFetch").val(), 'page' : page}),
    		  success: function(data) {			
				$("ul.books").hide().empty().append(data.list).slideDown("fast");
				$("#fetchSource").html("Showing results fetched from amazon.com");
				if (data.hasNext && data.hasPrev)
					$("#fetchPagination").html("<a id='next' title='Next'  href='javascript:void(0)' class='"+data.nextPage+"'>Next Page</a> <a href='javascript:void(0)' title='Previous' id='prev' class='"+data.prevPage+"'>Prev Page</a>");	
				else if (data.hasNext)
					$("#fetchPagination").html("<a href='javascript:void(0)' title='Next' id='next' class='"+data.nextPage+"'>Next Page</a>");
				else if (data.hasPrev)
					$("#fetchPagination").html("<a href='javascript:void(0)' title='Previous' id='prev' class='"+data.prevPage+"'>Prev Page</a>");
    		  }}); 
	});
	
	$("#manualISBNSwitch").toggle(function(){
		$("#manualISBNDiv").slideDown();
		$("#manualISBNSwitch").text("-_-|| Close it! I'd better try amazon :P");
	},function(){
		$("#manualISBNDiv").slideUp();
		$("#manualISBNSwitch").text(":o It’s not there? Let’s add it manually via ISBN");
		
	});
});